$().ready(function() {
	$("input[type='text']").attr("autocomplete","off");
	$("#myForm_login").validate();	
	$(".btn-login").click(function(){
		var msg_error = '';
		var strEmail = $("#strEmail").val();
		var strPassword = $("#strPassword").val();
		if($.trim(strEmail)==""){
			msg_error = "Empty email.";
			$('#myForm_login').append('<div class="result-message-box"></div>');
		    $( ".result-message-box" ).dialog({
		    	title: "Message",
		    	autoOpen: false, 
		    	minHeight: 140,
				modal: true,
				draggable: false ,
				show: {
					effect: "explode",
					duration: 300
				},
				hide: {
					effect: "explode",
					duration: 300
				},	      
				buttons: {
					"Ok": function() {
						$(this).dialog("close");
						$( ".result-message-box" ).remove();
					}
				}
		      });
		    $('.ui-dialog-content').text(msg_error);
			$( ".result-message-box" ).dialog("open");	
		} else if($.trim(strPassword)==""){
			msg_error = "Empty password.";
			$('#myForm_login').append('<div class="result-message-box"></div>');
		    $( ".result-message-box" ).dialog({
		    	title: "Message",
		    	autoOpen: false, 
		    	minHeight: 140,
				modal: true,
				draggable: false ,
				show: {
					effect: "explode",
					duration: 300
				},
				hide: {
					effect: "explode",
					duration: 300
				},	      
				buttons: {
					"Ok": function() {
						$(this).dialog("close");
						$( ".result-message-box" ).remove();
					}
				}
		      });
		    $('.ui-dialog-content').text(msg_error);
			$( ".result-message-box" ).dialog("open");	
		} else {
			login_admin();
		}
	});
	$(window).resize(function(){
		hideSidebar();
	});
	hideSidebar();
	chk_login();
	bind_func();
});
function hideSidebar(){
	var wrapwidth = 900;
	var windowwidth = $(window).width();
	if(wrapwidth >= windowwidth){
		$('.left-wrapper-dashboard').hide();
		$('.responsive-wrapper-dashboard').show();
		$('#mobile_nav').show();
	} else {
		$('.left-wrapper-dashboard').show();
		$('#mobile_nav').hide();
	}
}
// ADMIN LOGIN
function login_admin(){
	var strEmail = $("#strEmail").val();
	var strPassword = $("#strPassword").val();
	
	xfunc = "login/login_user";
	
	data_sent = {
		'strEmail':strEmail,
		'strPassword':strPassword
	};
	
	$.ajax({ 
		type: "POST", // request type POST
		url: base_url+xfunc, // post to PHP file
		data: data_sent, // request send to PHP file
		dataType:'json',
		success: function(result){ // result = return from php request
			$('#myForm_login').append('<div class="result-message-box"></div>');
		    $( ".result-message-box" ).dialog({
		    	title: "Message",
		    	autoOpen: false, 
		    	minHeight: 140,
				modal: true,
				draggable: false ,
				show: {
					effect: "explode",
					duration: 300
				},
				hide: {
					effect: "explode",
					duration: 300
				},	      
				buttons: {
					"Ok": function() {
						$(this).dialog("close");
						$( ".result-message-box" ).remove();
					}
				}
		      });
		    $('.ui-dialog-content').text(result.message);
			$( ".result-message-box" ).dialog("open");
			if(result.chk_log==1){
				var strRememberMe = $('#strRememberMe').prop("checked");
				if(strRememberMe==true){
					$.cookie('owner', result.owner, { expires: 9999 });
					$.cookie('chk_log', result.chk_log, { expires: 9999 });
					$.cookie('intAccountType', result.intAccountType, { expires: 9999 });
					$.cookie('strName', result.strName, { expires: 9999 });
					$.cookie('session_code', result.session_code, { expires: 9999 });
					$.cookie('dateConceptionDate', result.dateConceptionDate, { expires: 9999 });
				} else {
					$.cookie('owner', result.owner, { expires: 1 });
					$.cookie('chk_log', result.chk_log, { expires: 1 });
					$.cookie('intAccountType', result.intAccountType, { expires: 1 });
					$.cookie('strName', result.strName, { expires: 1 });
					$.cookie('session_code', result.session_code, { expires: 1 });
					$.cookie('dateConceptionDate', result.dateConceptionDate, { expires: 1 });
				}
				chk_login();
				var strName = $.cookie('strName');	
				$("input[type='text']").val("");
				$("input[type='password']").val("");
				$(".error-msg").html("&nbsp;");
				$('.login-part').hide("slide", { direction: "left" }, 1000, function(){});
				
				$(".box-wrapper-login").hide("slide", { direction: "right" }, 1000,function(){					
					$(".admin-wrapper-box").fadeIn('slow',function(){
						$('#admin_name').html('Welcome '+strName);
						
					});
				});
			}
		}
	});	
}
function chk_login() {
	var owner = $.cookie('owner');
	var chk_log = $.cookie('chk_log');
	var intAccountType = $.cookie('intAccountType');
	var strName = $.cookie('strName');

	if(intAccountType==0){
		var newLocation = base_url;
		window.location = newLocation;
	} 
	if(chk_log==1){
		$("input[type='text']").val("");
		$("input[type='password']").val("");
		$(".error-msg").html("&nbsp;");
		
		/*$(".box-wrapper-login").fadeOut('slow',function(){					
			$(".admin-wrapper-box").fadeIn('slow',function(){
				$('#admin_name').html('Welcome '+strName);
			});
		});*/
		$(".login-part, .box-wrapper-login").hide();
		$(".admin-wrapper-box").show();
		$('#admin_name').html('Welcome '+strName);
	}
}
function togglenav(id){
	if("checkuplog"==id){
		$(".loader-"+id).show();
		list_checkuplog();
	} else if("trimesterguide"==id) {
		$(".loader-"+id).show();
		list_trimesterguide();
	} else if("diary"==id) {
		$(".loader-"+id).show();
		list_diary();
	} else if("tipoftheday"==id) {
		$(".loader-"+id).show();
		list_tipoftheday();
	} else if("checklist"==id) {
		$(".loader-"+id).show();
		checklist();
	} else if("topuser"==id) {
		$(".loader-"+id).show();
		top_user();
	} else if("obgynedirectory"==id) {
		$(".loader-"+id).show();
		obgyne_directory();
	} else if("adsbanner"==id) {
		$(".loader-"+id).show();
		ads();
	} else if("settings"==id) {
		$(".loader-"+id).show();
		list_settings();
	} else if("home"==id) {
		$(".loader-"+id).show();
	} else if("users"==id){
		users();
	} else if("usersadmin"==id){
		usersadmin();
	} else if("promo"==id){
		$(".loader-"+id).show();
		promo();
	} else {
		//do nothing
	}
	setTimeout("slideContent(\""+id+"\")", 1000);
	
}
function slideContent(id){
	$(".nav-control").hide();
	$(".jtn-dashboard-"+id).show("slide", { direction: "up" }, 500, function(){
		$(".loader-"+id).hide();
	});
}
function get_hash(){
	if(window.location.hash) {
		var func = window.location.hash;
		func = func.substring(1, func.length)
		$('#hash_btn').attr('onclick',func+"();");
		var chk_log = $.cookie('chk_log');
		if(chk_log==1){
			setTimeout("click_func()",1000);
		}
	} else {
	  // Fragment doesn't exist
	}
}
function click_func(){
	$('.hide').hide();
	$('#hash_btn').click();
}
function bind_func(){
	$('.sign-out-btn').click(function(){
	    $( "#box-wrapper-message" ).dialog({
	    	title: "Sign Out",
	    	autoOpen: false,
			height: 140,
			modal: true,
			draggable: false ,
			show: {
				effect: "explode",
				duration: 300
			},
			hide: {
				effect: "explode",
				duration: 300
			},	      
			buttons: {
				"Yes": function() {
					signoff();
					$(this).dialog("close");
				},
				"No": function() {
					$(this).dialog("close");
				}
			}
	      });
	    $('.ui-dialog-content').text("Are you sure?");
		$( "#box-wrapper-message" ).dialog("open");
	});
}
function signoff(){
	$.cookie('owner', null);
	$.cookie('chk_log', null);
	$.cookie('intAccountType', null);
	$.cookie('strName', null);
	$.cookie('session_code', null);
	$.cookie('dateConceptionDate', null);
	$('.admin-wrapper-box').hide();
	setTimeout(function(){ 
					$('.box-wrapper-login').show();
					$('.box-wrapper-login').fadeIn("fast", function(){
					});
			},
			1000);
}